#r @"C:\Subversion\textservices\TextServices\src\TSDotNet\TSDotNet.Encoders\bin\Debug\TSDotNet.Common.dll"
#r @"C:\Subversion\textservices\TextServices\src\TSDotNet\TSDotNet.Encoders\bin\Debug\TSDotNet.Encoders.dll"

open System
open System.Data
open System.Data.SqlClient
open System.Data.Sql
open TSDotNet.Encoders

let genSqlDicts () =
    [0x0..0xFF]
        |> List.iter (fun v -> (printf "%02X, " v; printfn "%d" v))

    let noOut x = [[0]; [128]; [130..140]; [142]; [145..156]; [158..159]] |> List.concat |> List.exists ((=) x)
    let noIn x = ((x &&& 0x0F) = 0) || (((x >>> 4) &&& 0x0F) = 0)

    let rec mapChar (oi, last) c =
        if noIn c then (oi, 0)
        elif noOut oi then mapChar (oi+1, last) c
        else (oi+1, oi)

    let byteToSql = [0..255] |> List.scan mapChar (0, 0) |> Seq.skip 1 (* Initial state! *) |> Seq.map snd |> Seq.toList
    let sqlToByte = [0..255] |> List.map (fun x -> try List.findIndex ((=) x) byteToSql with | _ -> 0)

    sqlToByte
//        |> Seq.zip [0..255]
//        |> Seq.map (sprintf "%A")
//        |> Seq.map (fun s -> s.Trim([|'('; ')'|]))
        |> Seq.iter (printf "%d, ")


let getTestCompressedSqlText s =
    let composite = new TSDotNet.Encoders.Latin1CsvCompressor();
    let zip = composite.Encode(s);
    let unzip = composite.Decode(zip);
    (zip, unzip)    

let getDbConn () =
    let csDEV     = "Server=qrvg-db-dev.systems.uk.hsbc,10020;Database=FinanceDDManagement;Application Name=PALMS.Dashboard;MultipleActiveResultSets=True;User ID=dpcwuser;Password=fsdjf4dfjsx7"
    let conn = new SqlConnection(csDEV)
    conn.Open()
    conn

let testSqlRoundTripChars () = 
    let conn = getDbConn ()
    let testChar c =
        let sIn = new String(c, 1) + "C"
        let comm = new SqlCommand("UPDATE TS_Tests SET Value = @Value WHERE Id = 262")
        comm.Parameters.Add(new SqlParameter("Value", sIn)) |> ignore
        comm.Connection <- conn
        let id = comm.ExecuteNonQuery() |> ignore

        let comm = new SqlCommand("SELECT Value FROM TS_Tests WHERE Id = 262")
        comm.Connection <- conn
        let sOut = comm.ExecuteScalar() :?> string

        if not(sOut.Equals(sIn)) then printfn "%d" (int c)  // "In: (%c / %d) - Out: (%s, / %d)" c (int c) sOut (int (sOut.Chars(0)))

    [0..255] |> List.map (char) |> List.iter testChar


let testSqlRoundTrip () = 
    let conn = getDbConn ()
    let s = "0.91,-13394.000594,0.45,0,0,0,-25206.111758,+31215.922354,41334.54218,-51816.167462,-66504.590744,-76135.131954,-92066.85092,-108795.383934,-117840.003658,-121399.473068,-130803.016416,-137750.956066,-148581.152292,-162206.598108,-174252.120814,-171964.879214,-176701.18506,-176885.451566,-181012.944746,-196608.356758,-205358.963636,-266085.341904,-303523.967672,-342422.403662,-374867.27061,-401375.034054,-457094.579386,-502268.466202,-526026.073054,-552884.766906,-567840.806426,-564791.081126,-593550.873594,-632752.471606,-678405.355556,-709651.498896,-748208.740946,-768746.908636,-744767.423288,-773808.877464,-822058.04012,-830124.21368,-883389.536804,-854137.863972,-893386.964366,-881535.718946,-912609.823238,-896619.890396,-928736.538774,-927957.75081,-944515.375324,-962142.701666,-1014604.498522,-1019191.208438,-1027119.38494,-989404.805202,-1008495.278566,-972736.1269,-885537.13764,-712208.111522,-539175.584218,-374383.347286,-207621.239086,-111942.116324,-40685.55953,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0"
    let (zip, unzip) = getTestCompressedSqlText s
    printfn "%s" zip
    let comm = new SqlCommand("INSERT INTO TS_Tests(Value) VALUES (@Value)")
    comm.Parameters.Add(new SqlParameter("Value", zip)) |> ignore
    comm.Connection <- conn
    comm.ExecuteNonQuery() |> ignore

    let zipper2 = new TSDotNet.Encoders.Latin1CsvCompressor()
    let comm = new SqlCommand("SELECT Value FROM TS_Tests WHERE Id = (SELECT TOP 1 Id FROM TS_Tests ORDER BY Id DESC)")
    comm.Connection <- conn
    let sqlOut = comm.ExecuteScalar() :?> string
    let sqlOutUnzip = zipper2.Decode(sqlOut)

    printfn "%s, %s, %b" unzip sqlOutUnzip (unzip.Equals(sqlOutUnzip))
    ()

do
    //testSqlRoundTripChars ()
    testSqlRoundTrip ()

    ()